package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.CashHistoryListAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.CashHistoryListObject;
import com.iexbuy.ihg.entity.CashHistoryListObjectResponce;
import com.iexbuy.ihg.entity.DealHistoryDetailListObjectResponce;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.StringUtil;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WeDeCashWithdrawFragment extends BaseFragment {

	private static WeDeCashWithdrawFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;
	@ViewInject(R.id.balance)
	private TextView balance;
	@ViewInject(R.id.amount)
	private EditText amount;
	@ViewInject(R.id.aliyPayAccount)
	private EditText aliyPayAccount;
	@ViewInject(R.id.aliyPayUserName)
	private EditText aliyPayUserName;

	private RelativeLayout listHeaderView;

	private List<CashHistoryListObject> CashHistoryListObjects = new ArrayList<CashHistoryListObject>();
	private int pageNo = 0;
	private CashHistoryListAdapter cashHistoryListAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_wede_cash_withdraw, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.we_de_cashs_withdraw));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		mBaseActivity.setRightButton("提现历史",18,new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				OpenHistoryPage();
			}
		});

		balance.setText(App.sPreferences.getCashBalance() + "元");
	}

	@OnClick(R.id.withdrawBtn)
	public void withdrewCashBtnClick(View view) {
		if (StringUtil.isEmpty(amount.getText().toString())) {
			ToastUtils.toast(mBaseActivity, "请输入提现金额");
			return;
		}
		if (StringUtil.isEmpty(aliyPayAccount.getText().toString())) {
			ToastUtils.toast(mBaseActivity, "请输入支付宝账号");
			return;
		}
		if (StringUtil.isEmpty(aliyPayUserName.getText().toString())) {
			ToastUtils.toast(mBaseActivity, "请输入支付宝姓名");
			return;
		}

		submit();
	}

	private void submit() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("userId", App.sPreferences.getUserId());
		paramMap.put("zhiFuBaoAcc", aliyPayAccount.getText().toString());
		paramMap.put("zhiFuBaoName", aliyPayUserName.getText().toString());
		paramMap.put("delta", amount.getText().toString());
		ServiceHelper.send(mBaseActivity, ServiceHelper.SUBMIT_CASH_TIXIANG, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						mBaseActivity.showDialog("加载中...");
					}
					@Override
					public void onCancelled() {
						mBaseActivity.closeDialog();
					}
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						mBaseActivity.closeDialog();
						CommUtil.showConfirmAlert(mBaseActivity, "提现请求保存成功!", new View.OnClickListener() {
							@Override
							public void onClick(View view) {
								OpenHistoryPage();
							}
						}, null);
					}
					@Override
					public void onFailure(HttpException error, String msg) {
						mBaseActivity.closeDialog();
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}

	private void OpenHistoryPage() {
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeCashWithdrawRecordsFragment.class);
		mBaseActivity.finish();
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static WeDeCashWithdrawFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
